package com.catchdays;

import java.sql.SQLException;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.widget.ImageButton;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.catchdays.db.DatabaseHelper;
import com.catchdays.db.model.CatchDaysModel;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.OptionsItem;
import com.googlecode.androidannotations.annotations.OptionsMenu;
import com.googlecode.androidannotations.annotations.OrmLiteDao;
import com.googlecode.androidannotations.annotations.ViewById;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;

@EActivity(R.layout.activity_view)
@OptionsMenu(R.menu.view)
public class ViewActivity extends SherlockActivity {
	private static final String TAG = "ViewActivity";

	@OrmLiteDao(helper = DatabaseHelper.class, model = CatchDaysModel.class)
	Dao<CatchDaysModel, Integer> catchDaysDao;

	@ViewById
	ImageButton favorite;

	@ViewById
	TextView title;

	@ViewById
	TextView anniversaryYear;

	@ViewById
	TextView anniversaryMonth;

	@ViewById
	TextView anniversaryDay;

	@ViewById
	TextView anniversaryType;

	private int id;
	private CatchDaysModel catchDays;
	private int favoriteYn;

	@AfterViews
	public void init() {
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setTitle(R.string.menu_view);

		Intent intent = getIntent();
		id = intent.getIntExtra("id", 0);

		try {
			catchDays = catchDaysDao.queryForId(id);
			favoriteYn = catchDays.getFavoriteYn();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		if (favoriteYn == 1) {
			favorite.setBackgroundResource(R.drawable.favorite_y);
		} else {
			favorite.setBackgroundResource(R.drawable.favorite_n);
		}

		title.setText(catchDays.getTitle());
		anniversaryYear.setText(catchDays.getAnniversaryYear() + "");
		anniversaryMonth.setText(catchDays.getAnniversaryMonth() + "");
		anniversaryDay.setText(catchDays.getAnniversaryDay() + "");
		anniversaryType.setText(catchDays.getAnniversaryType() + "");

	}

	@Click
	public void favorite() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(R.string.message_title_guide);

		if (favoriteYn == 1) {
			builder.setMessage(R.string.message_cancel_favortie);
			builder.setPositiveButton(R.string.message_ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						UpdateBuilder<CatchDaysModel, Integer> updateBuilder = catchDaysDao.updateBuilder();
						updateBuilder.where().eq("id", id);
						updateBuilder.updateColumnValue("favoriteYn", 0);

						int result = updateBuilder.update();
						if (result != 0) {
							favorite.setBackgroundResource(R.drawable.favorite_n);
							favoriteYn = 0;
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		} else {
			builder.setMessage(R.string.message_add_favorite);
			builder.setPositiveButton(R.string.message_ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						UpdateBuilder<CatchDaysModel, Integer> updateBuilder = catchDaysDao.updateBuilder();
						updateBuilder.where().eq("id", id);
						updateBuilder.updateColumnValue("favoriteYn", 1);

						int result = updateBuilder.update();
						if (result != 0) {
							favorite.setBackgroundResource(R.drawable.favorite_y);
							favoriteYn = 1;
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		}

		builder.setNegativeButton(R.string.message_cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});

		AlertDialog alert = builder.create();
		alert.show();
	}

	@OptionsItem
	public void menuEdit() {
		AlertDialog alert = new AlertDialog.Builder(this).setTitle(R.string.message_title_guide).setMessage(R.string.message_go_edit).setPositiveButton(R.string.message_ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(getApplicationContext(), AddActivity_.class);
				intent.putExtra("id", catchDays.getId());
				startActivity(intent);

				finish();
			}
		}).setNegativeButton(R.string.message_cancel, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		}).create();

		alert.show();
	}

	@OptionsItem
	public void homeSelected() {
		finish();
	}

	@Override
	public void onBackPressed() {
		finish();
	}
}
